package com.jill.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils{

    private static final String SECRET_KEY = "JILL";
    private static final Long EXPIRATION_TIME = 12*60*60*1000L;

    public static String GenerateToken(String username,String password){
        Map<String, Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        String token= Jwts.builder().signWith(SignatureAlgorithm.HS256, SECRET_KEY)//指定加密算法以及密钥
                .addClaims(map)//增添数据
                .setExpiration(new Date(System.currentTimeMillis()+EXPIRATION_TIME))//令牌截至时间
                .compact();//构建令牌
        return token;
    }

    public static Claims ParseToken(String token){
        Claims data=Jwts
                .parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();

        return data;
    }
}
